/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

 

function uploadfile() {
	if (!checkFile()) {
		return false;
	}
	if (!checkFileAviable()) {
		return false;
	}
	f$("uploadBtn").disabled = true;
	f$("processBar").style.width = 0;
	requestFileStatus();
	f$("uploadForm").submit();
}

function changefile(obj) {
	if (obj.value != "") {
		var k = obj.value.lastIndexOf("\\") + 1;
		f$("picname").value = obj.value.substring(k);
	}
}
function checkFile() {
	if (f$("picpath").value == "") {
		alert("文件不能为空");
		return false;
	}
	if (f$("picname").value == "") {
		alert("文件名不能为空");
		return false;
	}
	return true;
}

function log(msg, logNode) {
	var vNode;
	if (logNode != null)
		vNode = logNode;
	else
		vNode = f$("log");
	vNode.innerHTML = vNode.innerHTML + msg + " <br>";
}

function checkFileAviable() {
	var ajax = new Ajax();
	var res = true;
	ajax.requestURL = "upload_ajax_check.action";
	ajax.method = "GET";
	ajax.setVar("flag", f$("flag").value);
	ajax.setVar("picname", f$("picname").value);
	ajax.onCompletion = function() {
		var resText = ajax.xmlhttp.responseText;
		if (resText == "0") {
			log("[" + f$("picname").value + "]非法的文件格式");
			res = false;
		} else if (resText == '-1') {
			log("[" + f$("picname").value + "]已经存在该文件了");
			res = false;
		} else if (resText == "1") {
			res = true;
		}
	}
	ajax.runAJAX(null, false);
	return res;
}

function requestFileStatus() {
	var ajax = new Ajax();
	ajax.requestURL = "upload_ajax.action";
	ajax.method = "GET";
	ajax.onCompletion = function() {
		var resText = ajax.xmlhttp.responseText;
		if (resText != null && resText != "") {
			if (resText.indexOf("!") == 0) {
				finishProcessBar(resText.substring(1));
			} else if (resText.indexOf("_") == 0) {
				window.setTimeout(function() {
					requestFileStatus();
				}, 100);
				renderProcessBar(resText.substring(1));			 
			}  else {
				renderError(resText);
			}
		}
	};
	ajax.runAJAX();
}
function finishProcessBar(resText) {
	var arr = resText.split(",");
	f$("processBar").style.width = 100 + "%";
	f$("processDesc").innerHTML = "上传[" + arr[0] + "/" + arr[1] + "] 当前速度["
			+ arr[2] + "],剩下时间[" + arr[3] + "],完成百分比[" + arr[4] + "%]";
	log("文件[" + f$("picname").value + "]上传完成,总共耗时[" + arr[5] + "]上传总大小["
			+ arr[1] + "]平均速度[" + arr[6] + "]");
	f$("uploadBtn").disabled = false;
	window.setTimeout(function() {
		f$("piclist").src = "page/admin/piclist.jsp";
	}, 1000);

}

function renderError(resText) {
	log(resText);
}

function renderProcessBar(resText) {
	var arr = resText.split(",");
	f$("processBar").style.width = arr[4] + "%";
	f$("processDesc").innerHTML = "上传[" + arr[0] + "/" + arr[1] + "] 当前速度["
			+ arr[2] + "],剩下时间[" + arr[3] + "],完成百分比[" + arr[4] + "%]";
}

function showbigpic(picpath) {
	window.open(picpath);
}

function delpic(id, picname) {
	if (!window.confirm("您确认删除?删除后无法恢复")) {
		return;
	}
	var ajax = new Ajax();
	ajax.requestURL = "admin_del_file.action";
	ajax.method = "GET";
	ajax.setVar("id", id);
	ajax.onCompletion = function() {
		var resText = ajax.xmlhttp.responseText;
		if (resText == "1") {
			log("[" + picname + "]删除成功!", window.parent.document
					.getElementById("log"));
		} else {
			log("[" + picname + "]删除不成功,失败原因[" + resText + "]",
					window.parent.document.getElementById("log"));
		}
		window.parent.document.getElementById("piclist").src = "page/admin/piclist.jsp";
	}
	ajax.runAJAX(null, false);
}

function batchdel() {
	var objs = f$("piclist").contentWindow.document
			.getElementsByName("choicePic");
	var arr_id = [];
	var arr_name = [];
	for ( var i = 0; i < objs.length; i++) {
		if (objs[i].checked) {
			var s = objs[i].value.split(",");
			arr_id.push(s[0]);
			arr_name.push(s[1]);
		}
	}
	if (arr_id.length == 0) {
		alert('您还未选中删除的对象');
		return;
	}
	var ajax = new Ajax();
	ajax.requestURL = "admin_del_file.action";
	ajax.method = "GET";
	ajax.setVar("action", "batch");
	ajax.setVar("ids", arr_id);
	ajax.onCompletion = function() {
		var resText = ajax.xmlhttp.responseText;
		if (resText == "1") {
			log("[" + arr_name + "]删除成功!");
		} else {
			log("[" + arr_name + "]删除不成功,失败原因[" + resText + "]");
		}
		f$("piclist").src = "page/admin/piclist.jsp";
	}
	ajax.runAJAX(null, false);
}

function choiceAll() {
	var objs = f$("piclist").contentWindow.document
			.getElementsByName("choicePic");
	for ( var i = 0; i < objs.length; i++) {
		if (objs[i].checked) {
			objs[i].checked = false;
		} else {
			objs[i].checked = true;
		}
	}
}